package com.polaris.live.adapter.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.lifecycle.LifecycleCoroutineScope
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.material.imageview.ShapeableImageView
import com.polaris.live.R
import com.polaris.live.adapter.view_holder.HomeListGameViewHolder
import com.polaris.live.common.adapter.BaseAdapter
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.ext.autoDp
import com.polaris.live.constant.LiveConst
import com.polaris.live.databinding.AdapterFriendShipBinding
import com.polaris.live.databinding.AdapterFriendTopRoomBinding
import com.polaris.live.databinding.AdapterHomeListGameBinding
import com.polaris.live.resp.back_resp.DisplayUserBean
import com.polaris.live.resp.back_resp.GameVo
import com.polaris.live.resp.bean.FriendTopBean
import com.polaris.live.utils.image.ImageUtils
import com.polaris.live.widget.country.countryIcons
import com.polaris.live.widget.refresh.BaseBindingViewHolder

/**
 * FriendShipRoomAdapter
 *
 * @author Created by 半仙 on 2024/11/26/026 14:55
 */
class FriendShipRoomAdapter(
    private val lifecycleScope: LifecycleCoroutineScope,
) : BaseAdapter<Any, BaseViewHolder>(0) {

    init {
        addChildClickViewIds(R.id.view_please_holder, R.id.cons_room_1, R.id.cons_room_2, R.id.cons_room_3)
    }

    override fun getDefItemViewType(position: Int): Int {
        return when (val item = data[position]) {
            is DisplayUserBean -> CommonConst.HomeListType.TYPE_NORMAL
            is FriendTopBean -> CommonConst.HomeListType.FRIEND_SHIP_TOP
            is GameVo -> CommonConst.HomeListType.TYPE_GAME
            else -> throw IllegalArgumentException("Unknown item type: $item")
        }
    }

    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): BaseBindingViewHolder<*, *> {
        val layoutInflater = LayoutInflater.from(parent.context)
        return when (viewType) {
            CommonConst.HomeListType.TYPE_NORMAL -> FriendShipAdapter.FriendShipViewHolder(
                AdapterFriendShipBinding.inflate(layoutInflater, parent, false), FriendShipAdapter.CHARM
            )

            CommonConst.HomeListType.FRIEND_SHIP_TOP -> FriendTopRoomViewHolder(
                AdapterFriendTopRoomBinding.inflate(layoutInflater, parent, false)
            )

            CommonConst.HomeListType.TYPE_GAME -> HomeListGameViewHolder(
                AdapterHomeListGameBinding.inflate(layoutInflater, parent, false),
                lifecycleScope
            )

            else -> throw IllegalArgumentException("Invalid view type: $viewType")
        }
    }

    class FriendTopRoomViewHolder(
        binding: AdapterFriendTopRoomBinding
    ) :
        BaseBindingViewHolder<AdapterFriendTopRoomBinding, FriendTopBean>(binding) {

        override fun bind(item: FriendTopBean) {
            val item0 = item.oneUserRoom
            val item1 = item.twoUserRoom
            val item2 = item.threeUserRoom
            mBinding.apply {
                //第一
                Glide.with(root.context).load(ImageUtils.imgPath(item0.cover))
                    .transform(CenterCrop(), RoundedCorners(8.autoDp)).into(itemRoomImage1)
                itemRoomName1.text = item0.roomName
                tvUserNum.text = item0.stayUser.toString()
                itemRoomLevel1.setCharmsLevel(item0.base.charms)
                userCountry1.setCountry(item0.base.country)
                widetListSub1.setUserSymbol(item0.symbol, showVis = View.GONE)
                setRoomType(item0.roomType, roomTag1)
                itemRoomPag1.setRoomFrame(item0.base.roomFrame)

                //第二
                Glide.with(root.context).load(ImageUtils.imgPath(item1.cover)).into(itemRoomImage2)
                itemRoomName2.text = item1.roomName
                tvUserNum2.text = item1.stayUser.toString()
                itemRoomLevel2.setCharmsLevel(item1.base.charms)
                userCountry2.setCountry(item1.base.country)
                widetListSub2.setUserSymbol(item1.symbol, 2, showVis = View.GONE)
                setRoomType(item1.roomType, roomTag2)
                itemRoomPag2.setRoomFrame(item1.base.roomFrame)

                //第三
                Glide.with(root.context).load(ImageUtils.imgPath(item2.cover)).into(itemRoomImage3)
                itemRoomName3.text = item2.roomName
                tvUserNum3.text = item2.stayUser.toString()
                itemRoomLevel3.setCharmsLevel(item2.base.charms)
                userCountry3.setCountry(item2.base.country)
                widetListSub3.setUserSymbol(item2.symbol, 2, showVis = View.GONE)
                setRoomType(item2.roomType, roomTag3)
                itemRoomPag3.setRoomFrame(item2.base.roomFrame)
            }
        }

        private fun setCountry(countryCode: String?, imageView: ShapeableImageView, llBottom: View) {
            if (countryCode != null) {
                val countryIcon = countryIcons[countryCode]
                if (countryIcon == null) {
                    llBottom.visibility = View.GONE
                } else {
                    llBottom.visibility = View.VISIBLE
                    imageView.setImageResource(countryIcon)
                }
            } else {
                llBottom.visibility = View.GONE
            }
        }

        private fun setRoomType(roomType: Int?, roomTag: ImageView) {
            when (roomType) {
                LiveConst.RoomType.NORMAL -> {
                    roomTag.visibility = View.INVISIBLE
                    roomTag.setImageResource(0)
                }

                LiveConst.RoomType.AGENCY -> {  //公会
                    roomTag.visibility = View.VISIBLE
                    roomTag.setImageResource(R.drawable.family_tag)
                }

                LiveConst.RoomType.OFFICIAL -> { //官方
                    roomTag.visibility = View.VISIBLE
                    roomTag.setImageResource(R.drawable.sala_tag)
                }

                else -> {
                    roomTag.visibility = View.INVISIBLE
                    roomTag.setImageResource(0)
                }
            }
        }
    }

    override fun convert(holder: BaseViewHolder, item: Any) {
        when (holder) {
            is FriendShipAdapter.FriendShipViewHolder -> if (item is DisplayUserBean) holder.bind(item)
            is FriendTopRoomViewHolder -> if (item is FriendTopBean) holder.bind(item)
            is HomeListGameViewHolder -> if (item is GameVo) holder.bind(item)
        }
    }
}